<template>
  <div class="app-container">
    <el-dialog
      title="检定/校准信息"
      :visible.sync="approverelevancy"
      width="800px"
      center
    >
      <el-form ref="installForm" :rules="rules" :model="installForm" label-width="150px">
        <el-row :gutter="12">
          <el-col :span="11">
            <el-form-item prop="lastDate" label="上次检定校准日期:">{{
              lastDate || ""
            }}</el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="cycle" label="检定/校准周期:">
              <span v-if="cycle == '1'">一个月</span>
              <span v-if="cycle == '2'">三个月</span>
              <span v-if="cycle == '3'">半年</span>
              <span v-if="cycle == '4'">一年</span>
              <span v-if="cycle == '5'">倆年</span>
              <span v-if="cycle == '6'">三年</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="12">
          <el-col :span="11">
            <el-form-item prop="checkName" label="本次检定/校准单位:">
              <el-input
                :disabled="isLook"
                v-model="installForm.checkName"
                style="width: 110%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="sendUser" label="本次送检人:">
              <el-input
                :disabled="isLook"
                v-model="installForm.sendUser"
                style="width: 110%"
              />
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item prop="sendDate" label="本次送检日期:">
              <el-date-picker
                :disabled="isLook"
                style="width: 110%"
                value-format="yyyy-MM-dd"
                v-model="installForm.sendDate"
                type="date"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="checkDate" label="本次检定校准日期:">
              <el-date-picker
                :disabled="isLook"
                style="width: 110%"
                value-format="yyyy-MM-dd"
                v-model="installForm.checkDate"
                type="date"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="result" label="结果及修正因子:">
              <el-input
                :disabled="isLook"
                v-model="installForm.result"
                style="width: 110%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item prop="number" label="检定/校准证书编号:">
              <el-input
                :disabled="isLook"
                v-model="installForm.number"
                style="width: 110%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item prop="files" label="上传附件:">
              <el-upload
                class="upload-demo"
                action="/api/deviceInstall/fileUpload"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-change="customUploadChange"
                :before-upload="beforeUpload"
                name="files"
                multiple
                :limit="3"
                :on-exceed="handleExceed"
                :file-list="fileList"
                @click="uploadFiles"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  （请上传检定/校准相关附件）
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="approverelevancy = false">取消</el-button>
        <el-button type="primary" @click="confirmUpdate">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "@/api/Tobecertified";
export default {
  computed: {},
  data() {
    return {
      lastDate: "",
      cycle: "",
      deviceId: "", //id
      visible: true,
      approverelevancy: false,
      isLook: false,
      fileList: [],
      files: "",
      rules: {
        checkName: [{ required: true, message: "本次检定/校准单位不能为空", trigger: "blur" }],
        sendDate: [ { required: true, message: "本次送检日期不能为空", trigger: "blur" }],
        checkDate: [ { required: true, message: "本次检定校准不能为空", trigger: "blur" }],
      },
      installForm: {
        lastDate: "",
        cycle: "",
        checkName: "",
        sendUser: "",
        checkDate: "",
        sendDate: "",
        result: "",
        number: "",
        files: "",
        deviceId: ""
      }
    };
  },
  methods: {
    beforeUpload(e) {
      // console.log(e);
      this.files = e;
    },
    uploadFiles(e) {
      // console.log(e.target.files);
    },
    handleRemove(files, fileList) {
      // console.log(files, fileList);
      this.fileList = fileList;
    },
    handlePreview(files) {
      // console.log(files);
    },
    handleExceed(files, fileList) {
      // console.log(files);
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(files, fileList) {
      return this.$confirm(`确定移除 ${files.name}？`);
    },
    //上传文件
    customUploadChange(files) {
      // console.log(files);
      if (files.status === "ready") return;
      if (files.status === "success") {
        const res = files.response || {};
        if (res && res.code === 200) {
          this.fileResult = files.response.result;
          // console.log(this.fileResult);
          this.$message({
            type: "success",
            message: "上传成功!"
          });
          this.uploadVisible = false;
        } else {
          this.$message.error("上传失败!");
          this.fileList = [];
        }
      } else {
        this.$message.error("上传失败!");
        this.fileList = [];
      }
    },

    confirmUpdate(e) {
      // console.log(e);
      this.$refs.installForm.validate(valid => {
        if (valid) {
          // console.log(this.files);
          let formData = new FormData();
          // console.log(formData);
          formData.append("files", this.files);
          this.installForm.cycle = this.cycle;
          this.installForm.deviceId = Number(this.deviceId);
          this.installForm.lastDate = this.lastDate;
          this.installForm.files = this.fileResult[0];

          api
            .Added(this.installForm)
            .then(request => {
              this.$notify({
                title: "成功",
                message: request.message,
                type: "success"
              });
              this.approverelevancy = false;
            })
            .catch(() => {});
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.install-title {
  display: flex;
  align-items: center;
  line-height: 30px;
  padding-left: 20px;
  color: #fff;
  background-color: #d7dae2;
  justify-content: flex-start;
  margin-bottom: 10px;
}
.delete {
  width: 10px;
  height: 10px;
  position: relative;
  top: -10px;
  cursor: pointer;
}
::v-deep .el-upload-list {
  width: 100px;
  display: flex;
  margin: 10px 10px;
}
::v-deep .el-upload-list > li {
  margin: 10px 10px;
}
::v-deep.el-upload-list__item {
  margin: 10px 10px;
}
::v-deep.el-upload-list--picture .el-upload-list__item {
  margin: 10px 10px !important;
}
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}

::v-deep.avatar-upload-preview {
  width: 400px;
  border-radius: 0%;
  height: 400px;
  > img {
    transform: translateX(0);
  }
}
.user-info-head:hover:after {
  content: "+";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  border-radius: 50%;
}
</style>
